//
//  HFCalendarView.m
//  HFCalendar
//
//  Created by Luffy on 2021/8/24.
//

#import "HFCalendarView.h"
#import "HFWeekMenuView.h"
#import "HFDaysMenuView.h"
#import "HFCalendarBottomView.h"

#import "HFCalendarHeader.h"

@interface HFCalendarView () <HFCalendarDelegate>

@property (nonatomic, strong) HFWeekMenuView *weekMenuView;
@property (nonatomic, strong) HFDaysMenuView *daysMenuView;
@property (nonatomic, strong) HFCalendarBottomView *bottomView;

@end

@implementation HFCalendarView {
    NSString *_startDateString;
    NSString *_endDateString;
    CGFloat _bottomHeight;
}

- (void)dealloc {
    [_bottomView removeObserver:self forKeyPath:@"contentStr"];
}

- (instancetype)initWithFrame:(CGRect)frame
              startDateString:(NSString *)startDateString
                endDateString:(NSString *)endDateString {
    self = [super initWithFrame:frame];
    if (self) {
        _startDateString = startDateString;
        _endDateString = endDateString;
        _bottomHeight = (kIPhoneX ? 70 : 50);
            
        [self setupUI];
    }
    return self;
}

#pragma mark - Intial Methods
- (void)setupUI {
    self.backgroundColor = UIColor.whiteColor;
        
    [self addSubview:self.weekMenuView];
    [self addSubview:self.bottomView];
    [self addSubview:self.daysMenuView];
    
    self.bottomView.contentStr = @"";
    [self.bottomView addObserver:self forKeyPath:@"contentStr" options:NSKeyValueObservingOptionNew context:nil];
    
    __weak typeof(self)weakSelf = self;
    
    self.bottomView.sure_callBack = ^(NSString * _Nonnull dateStr) {
        if (weakSelf.sure_callBack) {
            weakSelf.sure_callBack(dateStr);
        }
    };
}

#pragma mark - Target Methods


#pragma mark -- 监听调用方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"选择的日期：%@",self.bottomView.contentStr);
}

#pragma mark - HFCalendarDelegate
- (void)clickCalendarWithStartDate:(NSString *)startDate
                        andEndDate:(NSString *)endDate {
    if (endDate.length > 0) {
        _bottomView.contentStr = [NSString stringWithFormat:@"%@-%@",startDate,endDate];
    } else {
        _bottomView.contentStr = [NSString stringWithFormat:@"%@",startDate];
    }
}

- (void)clearCalendarDate {
    _bottomView.contentStr = @"";
}

#pragma mark - Setter Getter Methods
- (HFWeekMenuView *)weekMenuView {
    if (!_weekMenuView) {
        _weekMenuView = [[HFWeekMenuView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    }
    return _weekMenuView;
}

- (HFCalendarBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[HFCalendarBottomView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - _bottomHeight, SCREEN_WIDTH, _bottomHeight)];
    }
    return _bottomView;
}

- (HFDaysMenuView *)daysMenuView {
    if (!_daysMenuView) {
        _daysMenuView = [[HFDaysMenuView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.weekMenuView.frame), SCREEN_WIDTH, self.frame.size.height - 40 - _bottomHeight) startDateString:_startDateString endDateString:_endDateString];
        _daysMenuView.maxChooseNumber = self.maxChooseNumber;
        _daysMenuView.delegate = self;
    }
    return _daysMenuView;
}

- (void)setDateArray:(NSArray *)dateArray {
    _dateArray = dateArray;
    
    if (dateArray.count > 1) {
        _bottomView.contentStr = [NSString stringWithFormat:@"%@-%@",dateArray[0],dateArray[1]];
    } else if (dateArray.count > 0) {
        _bottomView.contentStr = [NSString stringWithFormat:@"%@",dateArray[0]];
    }
    
    self.daysMenuView.dateArray = self.dateArray;
}

@end
